package org.itboys.product.web;

import com.google.common.collect.Lists;
import org.apache.commons.lang3.time.DateUtils;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mongodb.utils.page.Page;
import org.itboys.product.dto.PromotionProductDTO;
import org.itboys.product.entity.Product;
import org.itboys.product.entity.PromotionProduct;
import org.itboys.product.service.ProductService;
import org.itboys.product.service.PromotionProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by Administrator on 2016/7/4.
 */
@RestController
@RequestMapping("/promotion/product")
public class PromotionProductController extends BaseController {

    @Autowired
    private ProductService productService;
    @Resource
    private PromotionProductService promotionProductService;


    /**
     * 获取商品列表 带分页参数
     * @param request
     * @param response
     */
    @RequestMapping("/list")
    public void list(HttpServletRequest request,HttpServletResponse response){
        Page<PromotionProduct> page = promotionProductService.pageQuery(request);
        List<PromotionProductDTO> dtoList = Lists.newArrayList();

        List<Long> productIdList = page.getData().stream()
                .map(PromotionProduct::getProductId).collect(Collectors.toList());
        List<Product> products = productService.getByIds(productIdList);
        Map<Long, String> id2NameMap = products.stream()
                .collect(Collectors.toMap(Product::getId, Product::getName));
        for (PromotionProduct product : page.getData()) {
            dtoList.add(transferDTO(product, id2NameMap.get(product.getProductId())));
        }
        JsonPageUtils.renderJsonPage(page.getTotal(), dtoList, response);
    }

    @ResponseBody
    @RequestMapping("/getPid/{id}")
    public void getOrg(@PathVariable("id")Long id,HttpServletResponse response){
        PromotionProduct promotionProduct = promotionProductService.getById(id);
        if (promotionProduct != null) {
            Product product = productService.getById(promotionProduct.getProductId());
            PromotionProductDTO dto = transferDTO(promotionProduct, product.getName());
            AjaxUtils.renderJson(response, dto);
        } else {
            AjaxUtils.renderJson(response, null);
        }
    }

    @ResponseBody
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public void save(@ModelAttribute PromotionProduct product , HttpServletResponse response){
        try {
            if(product.getId() == 0l){
                promotionProductService.save(product);
            }else {
                promotionProductService.update(product);
            }
            AjaxUtils.renderText(response, CommonConstants.SUCCESS);
        } catch (Exception e) {
            AjaxUtils.renderText(response, CommonConstants.FAIL);
        }
    }

    @ResponseBody
    @RequestMapping("/delete/{id}")
    public void delete(@PathVariable("id")Long id,HttpServletResponse response){
        try {
            promotionProductService.delete(id);
            AjaxUtils.renderText(response, CommonConstants.SUCCESS);
        } catch (Exception e) {
            AjaxUtils.renderText(response, CommonConstants.FAIL);
        }
    }

    @ResponseBody
    @RequestMapping("/addToPromotion")
    public void addToPromotion(@PathVariable("id")Long id,HttpServletResponse response){

    }

    private PromotionProductDTO transferDTO(PromotionProduct promotionProduct, String productName) {
        PromotionProductDTO dto = new PromotionProductDTO();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dto.setProductId(promotionProduct.getProductId());
        dto.setPrice(promotionProduct.getPrice());
        dto.setStartTime(promotionProduct.getStartTime());
        dto.setEndTime(promotionProduct.getEndTime());
        dto.setId(promotionProduct.getId());
        dto.setLimitCount(promotionProduct.getLimitCount());
        dto.setProductName(productName);
        return dto;
    }
}
